package com.zhipu.balance.ui.vm.home.system

import android.content.Intent
import android.provider.Settings
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupAnimation
import com.zhipu.balance.mobile.temporaryClass.VersionUpdate
import com.zhipu.balance.pop.GeneralPurposePopup
import com.zhipu.balance.pop.IntranetSettingsPopup
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.home.system.SystemSettingActivity
import com.zhipu.balance.ui.home.system.device.DeviceActivity
import com.zhipu.balance.ui.home.system.number.NumberManagementActivity
import com.zhipu.balance.ui.home.system.personnel.PersonnelActivity
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.dialog.CommonDialog
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 系统设置
 */
class SystemSettingViewModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService) {


    /**
     * WIFI设置
     */
    fun wIFISetting(){
        val wifiSettingsIntent = Intent(Settings.ACTION_WIFI_SETTINGS)
        wifiSettingsIntent.putExtra("extra_prefs_show_button_bar", true)
        wifiSettingsIntent.putExtra("extra_prefs_set_next_text", "完成")
        ActivityUtils.startActivity(wifiSettingsIntent)
    }

    /**
     * 检测人员管理
     */
    fun InspectionPersonnelMnagement(){
        ActivityUtils.startActivity(PersonnelActivity::class.java)
    }

    /**
     * 内网设置
     */
    fun IntranetSettings(){
        var mIntranetSettingsPopup = IntranetSettingsPopup()
        XPopup.Builder(ActivityUtils.getTopActivity())
            .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
            .autoOpenSoftInput(false)
            .asCustom(mIntranetSettingsPopup)
            .show();
    }


    /**
     * 检测更新
     */
    fun DetectionUpdate(){
        var observable = mSystemApiService.version()
        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(object :
            Observer<VersionUpdate> {

            override fun onSubscribe(d: Disposable) {


            }

            override fun onNext(versionUpdate: VersionUpdate) {
                CommonDialog.showAccordingToUpdate(versionUpdate)
            }


            override fun onError(e: Throwable) {

            }


            override fun onComplete() {
            }
        })
    }



    /**
     * 设备信息
     */
    fun DeviceInformation(){
        ActivityUtils.startActivity(DeviceActivity::class.java)
    }


    /**
     * 编号管理
     */
    fun NumberManagement(){
        ActivityUtils.startActivity(NumberManagementActivity::class.java)
    }


    /**
     * 获取设备数据
     */
    fun ObtainDeviceData(){
        InitializeUtils.getDefaultMaker()?.show("功能暂时未开放")
    }



    /**
     * 关闭程序
     */
    fun CloseProcedure(){
        ActivityUtils.finishAllActivities()
        AppUtils.exitApp()
    }









    /**
     * 主菜单
     */
    fun mainMenu(){
        ActivityUtils.finishActivity(SystemSettingActivity::class.java)
    }
}